<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>Tcl_GetIndexFromObj manual page - Tcl Library Procedures</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl C API</a> <small>&gt;</small> GetIndex</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<DL>
<DD><A HREF="GetIndex.htm#M2" NAME="L366">NAME</A>
<DL><DD>Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct &mdash; lookup string in table of keywords</DD></DL>
<DD><A HREF="GetIndex.htm#M3" NAME="L367">SYNOPSIS</A>
<DL>
<DD><B>#include &lt;tcl.h&gt;</B>
<DD>int
<DD><B>Tcl_GetIndexFromObj</B>(<I>interp, objPtr, tablePtr, msg, flags,
<DD>indexPtr</I>)
<DD>int
<DD><B>Tcl_GetIndexFromObjStruct</B>(<I>interp, objPtr, structTablePtr, offset,
<DD>msg, flags, indexPtr</I>)
</DL>
<DD><A HREF="GetIndex.htm#M4" NAME="L368">ARGUMENTS</A>
<DL class="arguments">
</DL>
<DD><A HREF="GetIndex.htm#M5" NAME="L369">DESCRIPTION</A>
<DD><A HREF="GetIndex.htm#M6" NAME="L370">SEE ALSO</A>
<DD><A HREF="GetIndex.htm#M7" NAME="L371">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
Tcl_GetIndexFromObj, Tcl_GetIndexFromObjStruct &mdash; lookup string in table of keywords
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>#include &lt;tcl.h&gt;</B><BR>
int<BR>
<B>Tcl_GetIndexFromObj</B>(<I>interp, objPtr, tablePtr, msg, flags,<BR>
indexPtr</I>)<BR>
int<BR>
<B>Tcl_GetIndexFromObjStruct</B>(<I>interp, objPtr, structTablePtr, offset,<BR>
<TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT>msg, flags, indexPtr</I>)<BR>
<H3><A NAME="M4">ARGUMENTS</A></H3>
<DL class="arguments">
<DT><A HREF="../TclLib/Interp.htm">Tcl_Interp</A> <B>*interp</B> (in)<DD>
Interpreter to use for error reporting; if NULL, then no message is
provided on errors.
<P><DT><A HREF="../TclLib/Object.htm">Tcl_Obj</A> <B>*objPtr</B> (in/out)<DD>
The string value of this value is used to search through <I>tablePtr</I>.
The internal representation is modified to hold the index of the matching
table entry.
<P><DT>const char *const <B>*tablePtr</B> (in)<DD>
An array of null-terminated strings.  The end of the array is marked
by a NULL string pointer.
Note that references to the <I>tablePtr</I> may be retained in the
internal representation of <I>objPtr</I>, so this should represent the
address of a statically-allocated array.
<P><DT>const void <B>*structTablePtr</B> (in)<DD>
An array of arbitrary type, typically some <B>struct</B> type.
The first member of the structure must be a null-terminated string.
The size of the structure is given by <I>offset</I>.
Note that references to the <I>structTablePtr</I> may be retained in the
internal representation of <I>objPtr</I>, so this should represent the
address of a statically-allocated array of structures.
<P><DT>int <B>offset</B> (in)<DD>
The offset to add to structTablePtr to get to the next entry.
The end of the array is marked by a NULL string pointer.
<P><DT>const char <B>*msg</B> (in)<DD>
Null-terminated string describing what is being looked up, such as
<B><A HREF="../TkCmd/option.htm">option</A></B>.  This string is included in error messages.
<P><DT>int <B>flags</B> (in)<DD>
OR-ed combination of bits providing additional information for
operation.  The only bit that is currently defined is <B>TCL_EXACT</B>.
<P><DT>int <B>*indexPtr</B> (out)<DD>
The index of the string in <I>tablePtr</I> that matches the value of
<I>objPtr</I> is returned here.
<P></DL>
<H3><A NAME="M5">DESCRIPTION</A></H3>
These procedures provide an efficient way for looking up keywords,
switch names, option names, and similar things where the literal value of
a Tcl value must be chosen from a predefined set.
<B>Tcl_GetIndexFromObj</B> compares <I>objPtr</I> against each of
the strings in <I>tablePtr</I> to find a match.  A match occurs if
<I>objPtr</I>'s string value is identical to one of the strings in
<I>tablePtr</I>, or if it is a non-empty unique abbreviation
for exactly one of the strings in <I>tablePtr</I> and the
<B>TCL_EXACT</B> flag was not specified; in either case
the index of the matching entry is stored at <I>*indexPtr</I>
and <B><A HREF="../TclCmd/catch.htm">TCL_OK</A></B> is returned.
<P>
If there is no matching entry,
<B><A HREF="../TclCmd/catch.htm">TCL_ERROR</A></B> is returned and an error message is left in <I>interp</I>'s
result if <I>interp</I> is not NULL.  <I>Msg</I> is included in the
error message to indicate what was being looked up.  For example,
if <I>msg</I> is <B><A HREF="../TkCmd/option.htm">option</A></B> the error message will have a form like
&ldquo;<B>bad option &#34;firt&#34;: must be first, second, or third</B>&rdquo;.
<P>
If <B>Tcl_GetIndexFromObj</B> completes successfully it modifies the
internal representation of <I>objPtr</I> to hold the address of
the table and the index of the matching entry.  If <B>Tcl_GetIndexFromObj</B>
is invoked again with the same <I>objPtr</I> and <I>tablePtr</I>
arguments (e.g. during a reinvocation of a Tcl command), it returns
the matching index immediately without having to redo the lookup
operation.  Note: <B>Tcl_GetIndexFromObj</B> assumes that the entries
in <I>tablePtr</I> are static: they must not change between
invocations.  If the value of <I>objPtr</I> is the empty string,
<B>Tcl_GetIndexFromObj</B> will treat it as a non-matching value
and return <B><A HREF="../TclCmd/catch.htm">TCL_ERROR</A></B>.
<P>
<B>Tcl_GetIndexFromObjStruct</B> works just like
<B>Tcl_GetIndexFromObj</B>, except that instead of treating
<I>tablePtr</I> as an array of string pointers, it treats it as a
pointer to the first string in a series of strings that have
<I>offset</I> bytes between them (i.e. that there is a pointer to the
first array of characters at <I>tablePtr</I>, a pointer to the second
array of characters at <I>tablePtr</I>+<I>offset</I> bytes, etc.)
This is particularly useful when processing things like
<B>Tk_ConfigurationSpec</B>, whose string keys are in the same place in
each of several array elements.
<H3><A NAME="M6">SEE ALSO</A></H3>
<B>prefix</B>, <B><A HREF="../TclLib/WrongNumArgs.htm">Tcl_WrongNumArgs</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/I.htm#index">index</A>, <A href="../Keywords/O.htm#option">option</A>, <A href="../Keywords/V.htm#value">value</A>, <A href="../Keywords/T.htm#table lookup">table lookup</A>
<div class="copy">Copyright &copy; 1997 Sun Microsystems, Inc.
</div>
</BODY></HTML>
